
package com.eyedog.video.webview;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import com.eyedog.library.base.BaseFragment;
import com.eyedog.video.R;

public class WebFragment extends BaseFragment implements View.OnClickListener {

    WebView webView;

    public static int FROM_GAME = 0;

    public static int FROM_VIDEO_LIST_ADV = 1;

    public static int FROM_SEARCH_ADV = 2;

    public View back, front, refresh;

    public static Bundle createBundle(int fromType, String url) {
        if (TextUtils.isEmpty(url)) {
            return null;
        }
        Bundle data = new Bundle();
        data.putString("URL", url);
        data.putInt("FROM_TYPE", fromType);
        return data;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {
        View layout = inflater.inflate(R.layout.layout_webview, container, false);
        return layout;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle data = getArguments();
        String url = null;
        int fromeType = 0;
        if (data != null) {
            url = data.getString("URL");
            fromeType = data.getInt("FROM_TYPE");
        }
        back = view.findViewById(R.id.ll_back);
        front = view.findViewById(R.id.ll_front);
        refresh = view.findViewById(R.id.ll_refresh);
        back.setOnClickListener(this);
        front.setOnClickListener(this);
        refresh.setOnClickListener(this);
        webView = (WebView) view.findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.getSettings().setSupportMultipleWindows(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.setWebChromeClient(new WebChromeClient());
        if (fromeType == FROM_VIDEO_LIST_ADV) {
            ImageView imageView = (ImageView) view.findViewById(R.id.iv_close_button);
            imageView.setVisibility(View.VISIBLE);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    getActivity().finish();
                }
            });
        }
        if (!TextUtils.isEmpty(url)) {
            webView.loadUrl(url);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                if (webView.canGoBack()) {
                    webView.goBack();
                }
                break;
            case R.id.ll_front:
                if (webView.canGoForward()) {
                    webView.goForward();
                }
                break;
            case R.id.ll_refresh:
                webView.reload();
                break;
        }
    }
}
